package com.byxy.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.byxy.dao.GuideDao;
import com.byxy.entity.Guide;

//@Controller 控制层
@Repository // Dao层
// @Service //Service层
// @Component //组件
public class GuideDaoImpl implements GuideDao {
	@Resource
	// @Autowired
	// @Inject
	private SessionFactory sessionFactory;

	@Override
	public void add(Guide s) {
		sessionFactory.getCurrentSession().save(s);

	}

	@Override
	public void delete(Guide s) {
		sessionFactory.getCurrentSession().delete(s);
	}

	@Override
	public void update(Guide s) {
		sessionFactory.getCurrentSession().update(s);
	}

	@Override
	public List<Guide> find() {
		// String hql = "select u from Guide u";
		// SQL select * from Guide
		return sessionFactory.getCurrentSession().createQuery("from " + Guide.class.getName()).list();
	}

	@Override
	public List<Guide> find(int pos, int rows) {
		return sessionFactory.getCurrentSession().createQuery("from " + Guide.class.getName()).setFirstResult(pos)
				.setMaxResults(rows).list();
	}

	@Override
	public Guide get(Integer id) {
		return (Guide) sessionFactory.getCurrentSession().load(Guide.class, id);
		// (Guide) sessionFactory.getCurrentSession().get(Guide.class, id);
	}

	@Override
	public long count() {
		// select count(*) from Guide
		return (Long) sessionFactory.getCurrentSession()
				.createQuery("select count(u) from " + Guide.class.getName() + " u").uniqueResult();
	}

	@Override
	public List<Guide> find(Map<String, Object> params) {
		String hql = "SELECT u FROM " + Guide.class.getName() + " u";
		Set<Entry<String, Object>> it = params.entrySet();
		if (it.size() > 0) {
			hql += " WHERE ";
			for (Entry<String, Object> entry : it) {
				hql = hql + "u." + entry.getKey() + " = " + entry.getValue() + " AND ";
			}
			hql = hql.substring(0, hql.length() - " AND ".length());
		}
		// SQL select * from User where loginName=? and password=?
		return sessionFactory.getCurrentSession().createQuery(hql).list();

	}

	@Override
	public List<Guide> find(int pos, int rows, Map<String, Object> params) {
		String hql = "SELECT u FROM " + Guide.class.getName() + " u";
		Set<Entry<String, Object>> it = params.entrySet();
		if (it.size() > 0) {
			hql += " WHERE ";
			for (Entry<String, Object> entry : it) {
				hql = hql + "u." + entry.getKey() + " = " + entry.getValue() + " AND ";
			}
			hql = hql.substring(0, hql.length() - " AND ".length());
		}
		return sessionFactory.getCurrentSession().createQuery(hql).setFirstResult(pos).setMaxResults(rows).list();
	}

	@Override
	public long count(Map<String, Object> params) {
		// select count(*) from User
		String hql = "SELECT count(u) FROM " + Guide.class.getName() + " u";
		Set<Entry<String, Object>> it = params.entrySet();
		if (it.size() > 0) {
			hql += " WHERE ";
			for (Entry<String, Object> entry : it) {
				hql = hql + "u." + entry.getKey() + " = " + entry.getValue() + " AND ";
			}
			hql = hql.substring(0, hql.length() - " AND ".length());
		}
		return (Long) sessionFactory.getCurrentSession().createQuery(hql).uniqueResult();
	}

}
